package com.carple.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carple.dao.BoardDao;
import com.carple.dto.Board;
import com.carple.ui.ThePager;

@WebServlet("/board/list.action")
public class BoardListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		int currentPage 	= 1;
		int pageSize		= 10;
		int dataCount		= 0;
		int pagerSize		= 3;
		String url 			= "list.action";
		//
		String page = request.getParameter("pageno");
		if(page != null && page.length() > 0){
			currentPage = Integer.parseInt(page);
		}
		int startRow		= (currentPage -1)*pageSize;
		
		BoardDao dao = new BoardDao();
		String header = request.getParameter("owner");
		System.out.println("servlet"+header);
		
		List<Board> boards = dao.getBoardList(startRow, pageSize, header);
		dataCount = dao.getBoardCount(header);//총 게시글의 
		
		
		Map<String, String[]> map = request.getParameterMap();
		
		//조회된 데이터를 jsp에서 처리할수 있도록 request객체에 저장
		request.setAttribute("boards", boards);
		ThePager pager = new ThePager(dataCount, currentPage, pageSize, pagerSize, url, map); 
		request.setAttribute("pager", pager);
				
		RequestDispatcher dispatcher = 
				request.getRequestDispatcher("/WEB-INF/views/board/boardlist.jsp");

			dispatcher.forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		doGet(request, response);
	}

}
